using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.CompositeUI.Utility;
using Jbe.TestSuite.Help.Constants;

namespace Jbe.TestSuite.Help
{
    public partial class HelpView : UserControl, IHelpView
    {
        public HelpView()
        {
            InitializeComponent();

            webBrowser.CanGoForwardChanged +=new EventHandler(WebBrowserChanged);
            webBrowser.CanGoBackChanged += new EventHandler(WebBrowserChanged);
        }

        protected override void OnLoad(EventArgs e)
        {
            presenter.OnViewReady();
            base.OnLoad(e);
            UpdateToolStripButtons();
        }

        public void UpdateHelpUrl(Uri url)
        {
            if (!url.Equals(webBrowser.Url))
            {
                webBrowser.Url = url;
            }
        }

        public void HomeUrlChanged()
        {
            UpdateToolStripButtons();
        }

        private void BackButtonClick(object sender, EventArgs e)
        {
            webBrowser.GoBack();
            Focus();
        }

        private void ForwardButtonClick(object sender, EventArgs e)
        {
            webBrowser.GoForward();
            Focus();
        }

        private void HomeButtonClick(object sender, EventArgs e)
        {
            webBrowser.Url = presenter.HomeUrl;
            Focus();
        }

        private void SaveButtonClick(object sender, EventArgs e)
        {
            webBrowser.ShowSaveAsDialog();
            Focus();
        }

        private void PrintButtonClick(object sender, EventArgs e)
        {
            webBrowser.ShowPrintPreviewDialog();
            Focus();
        }

        private void WebBrowserChanged(object sender, EventArgs e)
        {
            UpdateToolStripButtons();
        }

        private void UpdateToolStripButtons()
        {
            backButton.Enabled = webBrowser.CanGoBack;
            forwardButton.Enabled = webBrowser.CanGoForward;
            homeButton.Enabled = (presenter.HomeUrl != null);
        }
    }
}

